#include "ClientUtils.h"
#include "DefaultUtils.h"
#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif

extern int errno;


int connectsock(const char* host,const char* service,const char* transport)
{
	struct hostent* phe;
	struct servent* pse;
	struct protoent* ppe;
	struct sockaddr_in sin;
	int s = 0,type;

	memset(&sin,0,sizeof(sin));
	sin.sin_family = AF_INET;

	pse = getservbyname(service,transport);
	if(pse)
		sin.sin_port = pse->s_port;
	else if((sin.sin_port = htons((unsigned short)atoi(service)))==0)
		errexit("can't get %s service entry\n",service);
    phe=gethostbyname(host);
	if(phe)
		memcpy(&sin.sin_addr,phe->h_addr,phe->h_length);
	else if((sin.sin_addr.s_addr = inet_addr(host))==INADDR_NONE)
		errexit("can't get %s host entry\n",host);

	if((ppe=getprotobyname(transport))==0)
		errexit("can't get %s protocol entry\n",transport);

	if(0==strcmp(transport,"udp"))
		type = SOCK_DGRAM;
	else
		type = SOCK_STREAM;

	s = socket(PF_INET,type,ppe->p_proto);
	if(s<0)
		errexit("can't create socket:%s\n",strerror(errno));

    if(connect(s,(struct sockaddr*)&sin,sizeof(sin))<0)
        errexit("can't connect to %s.%s:%s \n",host,service,strerror(errno));

	return s;

}

int connectTCP(const char* host,const char* service)
{
	return connectsock(host,service,"tcp");
}

int connectUDP(const char* host,const char* service)
{
	return connectsock(host,service,"udp");
}


